package com.yunchang.springboot.scheduled.mwsfinance;

import com.yunchang.springboot.base.BaseService;
import com.yunchang.springboot.mapper.mwsfinance.FMonthlyStorageFeeMapper;
import com.yunchang.springboot.mapper.mwsreport.MwsDailyInventoryHistoryMapper;
import com.yunchang.springboot.mapper.mwsreport.MwsMonthlyStorageFeesMapper;
import com.yunchang.springboot.models.mwsfinance.FMonthlyStorageFee;
import com.yunchang.springboot.models.mwsreportbean.MwsMonthlyStorageFees;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * mws月度仓储费
 */
@Component
@Configuration
@Service
@Slf4j
@EnableScheduling
public class
FMonthlyStorageFeeScheduled extends BaseService {

    private static final String DEBIT_SUBJECT_ID = "660120";

    private static final String CREDIT_SUBJECT_ID = "220203";

    static  Integer  LIMIT_VALUE = 10000;

    static String DOCUMENTS_ID = "YCCFYD";
    /**
     * 月储存费用
     */
    private MwsMonthlyStorageFeesMapper mwsMonthlyStorageFeesMapper;

    @Autowired
    public void setMwsMonthlyStorageFeesMapper(MwsMonthlyStorageFeesMapper mwsMonthlyStorageFeesMapper) {
        this.mwsMonthlyStorageFeesMapper = mwsMonthlyStorageFeesMapper;
    }

    /**
     * 每日库存记录
     */
    private MwsDailyInventoryHistoryMapper mwsDailyInventoryHistoryMapper;

    @Autowired
    public void setMwsDailyInventoryHistoryMapper(MwsDailyInventoryHistoryMapper mwsDailyInventoryHistoryMapper) {
        this.mwsDailyInventoryHistoryMapper = mwsDailyInventoryHistoryMapper;
    }

    /**
     * 财务月度仓储费
     */
    private FMonthlyStorageFeeMapper fMonthlyStorageFeeMapper;

    @Autowired
    public void setFMonthlyStorageFeeMapper(FMonthlyStorageFeeMapper fMonthlyStorageFeeMapper) {
        this.fMonthlyStorageFeeMapper = fMonthlyStorageFeeMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void getFMonthlyStorageFee() {
        List<MwsMonthlyStorageFees> mwsMonthlyStorageFeesList = mwsMonthlyStorageFeesMapper.selectByFinanceStatus(1,null);
        Integer mwsMonthlyStorageFeesListSize = mwsMonthlyStorageFeesList.size();
        log.info("原始数据的数量为："+mwsMonthlyStorageFeesListSize);
        int forNum = 1;
        if(mwsMonthlyStorageFeesListSize>LIMIT_VALUE){
            forNum += mwsMonthlyStorageFeesListSize/LIMIT_VALUE;
            for(;forNum > 0;forNum--) {
                log.info("剩余几次处理："+forNum);
                mwsMonthlyStorageFeesList = mwsMonthlyStorageFeesMapper.selectByFinanceStatus(1, LIMIT_VALUE);
                fMonthlyStorageFeeDispose(mwsMonthlyStorageFeesList);
            }
        }else {
            fMonthlyStorageFeeDispose(mwsMonthlyStorageFeesList);
        }

    }

    public void fMonthlyStorageFeeDispose(List<MwsMonthlyStorageFees> mwsMonthlyStorageFeesList){
        if (null != mwsMonthlyStorageFeesList && !mwsMonthlyStorageFeesList.isEmpty()) {
            List<Object> fMonthlyStorageFeeList = new ArrayList<>(mwsMonthlyStorageFeesList.size());
            for (MwsMonthlyStorageFees mwsMonthlyStorageFees : mwsMonthlyStorageFeesList) {
                String sellerId = mwsMonthlyStorageFees.getSellerId();
                String fnSku = mwsMonthlyStorageFees.getFnsku();
                String monthOfCharge = mwsMonthlyStorageFees.getMonthOfCharge();
                String countryCode = mwsMonthlyStorageFees.getCountryCode();
                FMonthlyStorageFee fMonthlyStorageFee = new FMonthlyStorageFee();
                fMonthlyStorageFee.setApprovalStatus(1);
                fMonthlyStorageFee.setDocumentsId(DOCUMENTS_ID + UUIDUtil.getUUID());
                fMonthlyStorageFee.setSellerId(sellerId);
                String month = mwsMonthlyStorageFees.getMonthOfCharge();
                fMonthlyStorageFee.setMonth(month);
                fMonthlyStorageFee.setDate(DateUtil.getSeventhDayOfNextMonth(month,"yyyy-MM-dd"));
                fMonthlyStorageFee.setFnsku(fnSku);
                fMonthlyStorageFee.setAsin(mwsMonthlyStorageFees.getAsin());
                fMonthlyStorageFee.setAmount(mwsMonthlyStorageFees.getEstimatedMonthlyStorageFee());
                fMonthlyStorageFee.setCurrency(mwsMonthlyStorageFees.getCurrency());
                String sku = getSku(fnSku, countryCode, sellerId, monthOfCharge);
                fMonthlyStorageFee.setSku(sku);
                fMonthlyStorageFee.setCountry(countryCode);
                fMonthlyStorageFee.setDebitSubjectId(DEBIT_SUBJECT_ID);
                fMonthlyStorageFee.setCreditSubjectId(CREDIT_SUBJECT_ID);
                fMonthlyStorageFee.setApprovalStatus(1);
                fMonthlyStorageFee.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fMonthlyStorageFee.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                mwsMonthlyStorageFees.setFinanceStatus(2);
                fMonthlyStorageFeeList.add(fMonthlyStorageFee);
            }
            try {
                baseInsertList(fMonthlyStorageFeeList);
                log.info("已把全部数据插入完成，待更新状态");
                mwsMonthlyStorageFeesMapper.updateFinanceStatusByMwsMonthlyStorageFeesList(mwsMonthlyStorageFeesList);
                log.info("状态已更新，齐活了");
            } catch (Exception e) {
                log.error("插入数据是出现错误，月储存费用" + e);
                // 使try catch代码块中的代码受到事务的管理
                throw new RuntimeException("插入数据是出现错误，月储存费用");
            }
        }else {
            log.info("未查询到对应的月储存费用数据列表，时间为"+DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 获取sku
     * @param fnSku
     * @param countryCode
     * @param sellerId
     * @param monthOfCharge
     * @return
     */
    public String getSku(String fnSku,String countryCode,String sellerId,String monthOfCharge){
        String sku = null;
        if(!StringUtils.isEmpty(fnSku) && !StringUtils.isEmpty(countryCode) && !StringUtils.isEmpty(sellerId) && !StringUtils.isEmpty(monthOfCharge)){
//            sku = mwsDailyInventoryHistoryMapper.selectSkuByFnSkuAndCountryCodeAndSellerIdAndMonthOfCharge(fnSku, countryCode, sellerId, monthOfCharge);
            sku = mwsDailyInventoryHistoryMapper.selectSkuBySellerIdAndFnSku(sellerId,fnSku);
            if(!StringUtils.isEmpty(sku)){
                return sku;
            }else {
                log.info("查询出的sku为空,月度仓储费");
            }
        }else {
            log.info("根据fnSku countryCode sellerId monthOfCharge 获取sku，四者中有为空的情况。"+"fnSku:"+fnSku+"countryCode:"+countryCode+"sellerId:"+sellerId+"monthOfCharge:"+monthOfCharge);
        }
        return sku;
    }

}
